package com.evil.application.util;

import com.alibaba.fastjson.JSON;
import com.evil.application.enums.order.OrderSpecialControlEnum;
import com.evil.application.mapper.ApplicationOrderDataMapper;
import com.evil.application.pojo.entity.ApplicationOrder;
import com.evil.common.application.dto.order.data.ControlDataValueDTO;
import com.evil.common.application.dto.order.data.OrderIdControlIdDataReqDTO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liyang
 * @date 2023-01-08 16:52
 */
@Component
@AllArgsConstructor
public class ApplicationOrderDataUtil {

    private final ApplicationOrderDataMapper applicationOrderDataMapper;

    /**
     * 获取订单发起人
     *
     * @param applicationOrder applicationOrder
     * @return Long
     */
    public Long getOrderApplicant(ApplicationOrder applicationOrder) {
        return applicationOrderDataMapper.isExistOrderControlFirstData(applicationOrder.getOrderId(), OrderSpecialControlEnum.CREATOR.getId())
                .flatMap(e -> JSON.parseArray(e.getOrderData(), ControlDataValueDTO.class).stream()
                        .map(ControlDataValueDTO::getId).findFirst())
                .orElse(applicationOrder.getCreatorId());
    }

    /**
     * 获取订单 指定控件 数据的id值
     *
     * @param orderIdControlIdDataReqDTO orderIdControlIdDataReqDTO
     * @return List
     */
    public List<Long> getOrderControlDataId(OrderIdControlIdDataReqDTO orderIdControlIdDataReqDTO) {
        return applicationOrderDataMapper.isExistOrderControlFirstData(orderIdControlIdDataReqDTO.getOrderId(), orderIdControlIdDataReqDTO.getControlId())
                .map(e -> JSON.parseArray(e.getOrderData(), ControlDataValueDTO.class).stream()
                        .map(ControlDataValueDTO::getId).toList())
                .orElse(new ArrayList<>());
    }
}
